/******************************************************************************
*
* This file is part of FreeTrack (http://code.google.com/p/qtfreetrack).
* BY Willy Gardiol (willy@gardiol.org)
*
* FreeTrack is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FreeTrack is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar.  If not, see <http://www.gnu.org/licenses/>
*
******************************************************************************/

#ifndef IMAGE_H
#define IMAGE_H

#include <QtGui>

#define COLOR_CHANNELS 3

class Image
{
public:
    // Static access member - use this to load an image from disk without knowing its type

    // Create an image object. If file already exists, it can be read or written.
    static Image* CreateImage( const QString& );
    // Create an image object loading from an existing file. If file is missing, returns NULL
    static Image* LoadImage(const QString&);
    // Free allocated image
    static void FreeImage( Image* );

protected:
    // Constructor is protected, you MUST use static members
    Image( const QString& );
    ~Image();

public:
    // Check if this is a valid image file (use only on existing files)
    virtual bool isValid();
    // get image size (works only after startReading or startWriting)
    int width() const;
    int height() const;

public: // read image
    // Read cycle: call this first...
    virtual bool startReading( int zoom, int& output_width, int& output_height );
    // ...then this until all image has been read!
    virtual uchar* readScanline();
    // Use this to test if image has been read completely
//    virtual bool readComplete();

public: // write image
    // Write cycle: call this first...
    virtual bool startWriting( int width, int height );
    // ...then this until all image has been written!
    virtual bool writeScanline( uchar* );
    // Use this to test if image has been read complately
//    virtual bool writeComplete();

public: // fast read into QImage
    // Quick function to read an image into a QImage
    virtual bool readImage( QImage* image,
                    const QRect& source_rect = QRect(),
                    const QRect& dest_rect = QRect(),
                    int zoom = 1 );

public: // fast write from QImage
    virtual bool writeImage( const QImage& image );

protected:
    // Store image filename
    QString filename;
    // Width and height of the image (this is the size after all kinds of zooms)
    long int image_width;
    long int image_height;

    bool reading;
    bool writing;
};

#endif // IMAGE_H
